#include <stdio.h>

#if 1
/******************传递结构成员******************/
#define LEN 20

struct names {
    char first[LEN];
    char last[LEN];
};

struct guy{
    struct names handle;
    char favfood[LEN];
    char job[LEN];
    float income;
};

int main(void){
    struct guy fellow[2] = {
        {
            {"Ewen","Villard"},
            "grilled salmon",
            "personality coach",
            68112.00
        },
        {
            {"Rodney","Swillbelly"},
            "tripe",
            "tabloid editor",
            432400.00
        }
    };
    struct guy * him; //这是一个指向结构的指针
    //fellow 是一个结构数组，这意味着fellow[0]是一个结构
    printf("address #1: %p #2: %p\n",&fellow[0],&fellow[1]);
    //告诉编译器该指针指向何处
    him = &fellow[0];
    //him+1 指向 fellow[1]
    printf("pointer #1: %p #2: %p\n",him,him+1);
    //如果him == &fellow[0],那么*him == fellow[0],所以fellow[0].income == (*him).income 这里必须使用圆括号，因为.优先级比*运算符高
    printf("him->income is :$%.2f   fellow[0].income is :$%.2f   (*him).income is :$%.2f\n",him->income,fellow[0].income,(*him).income);
    
    him++;//指向下一个结构
    printf("him->favfood is %s: him->handle.last is %s\n",him->favfood,him->handle.last);

    return 0;
}

#else
/**************传递结构的地址***************/
#define FUNDLEN 50

struct funds{
    char bank[FUNDLEN];
    double bankfund;
    char save[FUNDLEN];
    double savefund;
};

double sum(const struct funds *);//参数是一个指针

int main(void){

    struct funds stan = {
        "Garlic-Melon Bank",
        4032.27,
        "Lucky's Savings and Loan",
        8543.94
    };
    //如果需要在被调函数中修改主调函数中成员的值，就要传递成员的地址
    printf("stan has a total of $%.2f.\n",sum(&stan));

    return 0;
}
//sum()函数使用指向funds结构的指针(money)作为它的参数。
//把地址&stan传递给该函数，使得指针money指向结构变量stan
//由于该函数不能改变指针所指向值的内容，所以把money声明为一个指向const的指针
double sum(const struct funds * money){
    return (money->bankfund + money->savefund);
}

#endif
